﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Data;
using MockPrjTeam4.Entity;
using MockPrjTeam4.Common;

namespace MockPrjTeam4.DAO
{
    public class UserDAO
    {
        public static User UserObject(IDataRecord read)
        {
            return new User
            {
                UserId = Methods.ConvertNumber(read["UserId"].ToString()),
                Account = Methods.ConvertString(read["Account"].ToString())
            };
        }
        public static IList<User> GetUser()
        {
            string query = string.Format("SELECT UserId, Account FROM [User]");
            IList<User> user = new List<User>();
            IDataReader read = SqlHelper.ExecuteReader(query);
            while (read.Read())
            {
                user.Add(UserObject(read));
            }
            return user;
        }
        public static User GetUserById(int id)
        {
            string query = string.Format("SELECT UserId, Account FROM [User] WHERE UserId=@UserId");
            var parameter = new Collection<SqlParameter>
            {
                new SqlParameter("@UserId", id)
            };
            IDataReader read = SqlHelper.ExecuteReader(query, parameter);
            if (read.Read())
                return UserObject(read);
            else return null;
        }
    }
}